package com.atguigu.spzx.manner.controller;

import com.atguigu.spzx.manner.service.SysMenuService;
import com.atguigu.spzx.manner.service.SystemUserService;

import com.atguigu.spzx.manner.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.LoginDto;
import com.atguigu.spzx.model.entity.AuthContextUtil;
import com.atguigu.spzx.model.entity.SysUser;
import com.atguigu.spzx.model.myemun.ResultCodeEnum;
import com.atguigu.spzx.model.vo.LoginVo;
import com.atguigu.spzx.model.vo.ValidateCodeVo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import spzx.util.AtguiguAssert;

import java.util.List;

@RestController
@RequestMapping(value = "/admin/system/index")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class IndexController {
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    ValidateCodeService validateCodeService;
    @Autowired
    SysMenuService sysMenuService;
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = systemUserService.login(loginDto);
        AtguiguAssert.notNull(loginVo,"用户名或密码错误");
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping(value = "/getUserInfo")

    public Result<SysUser> getUserInfo(@RequestHeader("token") String token) {
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }
    @GetMapping("/menus")
    public Result menus() {
   List<SysMenuVo>sysMenuVoList= sysMenuService.findUserMenus();
return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }

}                                                                                                                                                                                                                                                               
